<?php
if (!defined("PATH_HD"))
    exit('No direct script access allowed');
// .-----------------------------------------------------------------------------------
// |  Software: [HDPHP framework]
// |   Version: 2013.01
// |      Site: http://www.hdphp.com
// |-----------------------------------------------------------------------------------
// |    Author: 向军 <houdunwangxj@gmail.com>
// | Copyright (c) 2012-2013, http://houdunwang.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// |   License: http://www.apache.org/licenses/LICENSE-2.0
// '-----------------------------------------------------------------------------------

/**
 * 视图处理抽象工厂
 * @package     view
 * @author      后盾向军 <houdunwangxj@gmail.com>
 */
final class ViewFactory {

    public static $viewFactory = ''; //静态工厂实例
    protected $driverList = array(); //驱动链接组

    private function __construct() {

    }

    /**
     * 返回工厂实例，单例模式
     */
    public static function factory($driver = null) {
        //只实例化一个对象
        if (self::$viewFactory == '') {
            self::$viewFactory = new viewFactory();
        }
        if (is_null($driver)) {
            $driver = ucfirst(strtolower(C("TPL_ENGINE")));
        }
        if (isset(self::$viewFactory->driverList[$driver])) {
            return self::$viewFactory->driverList[$driver];
        }
        self::$viewFactory->getDriver($driver);
        return self::$viewFactory->driverList[$driver];
    }

    /**
     * 获得数据库驱动接口
     * @param string $driver
     */
    public function getDriver($driver) {
        if (isset($this->driverList[$driver])) {
            return $this->driverList[$driver];
        }
        $class = $driver . "View";
        $classFile = PATH_HD . '/libs/driver/view/driver/' . $class . '.class.php'; //类文件
        class_exists($class) || load_file($classFile); //加载类文件
        $this->driverList[$driver] = new $class(); //视图操作引擎对象
    }

    /**
     * 释放连接驱动
     */
    private function close() {
        foreach ($this->driverList as $db) {
            $db->close();
        }
    }

    /**
     * 析构函数
     * Enter description here ...
     */
    function __destruct() {
        $this->close();
    }

}

